package csc.traning.wpsj.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.traning.wpsj.dao.User;

public class CreateFolderServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			String folderName = req.getParameter("folderName");
			HttpSession sess = req.getSession(false);
			String currentDir = (String) sess.getAttribute("currentDir");
			if (folderName!=null) {
				
				
				
				User user = (User) sess.getAttribute("user");
				String userPath = "users/" + user.getUserName() + "/file_uploaded";
				String realUserPath =  getServletContext().getRealPath(userPath);
				
				String newPath = realUserPath + currentDir + "/" + folderName;
				System.out.println(newPath);
				
				File newDir = new File(newPath);
				if(!newDir.isDirectory()) {
					newDir.mkdirs();
				}
			}
			req.setAttribute("forwardDir", currentDir);
			RequestDispatcher dispatcher = req.getRequestDispatcher("file-management");
			dispatcher.forward(req, resp);

	}
	
}
